/*
 * SubmissionDialog.java
 *
 * Created on 03.02.2009, 21:57:32
 */

package de.blitzcoder.collide.error;

import de.blitzcoder.collide.Config;
import de.blitzcoder.collide.Interface;
import de.blitzcoder.collide.gui.IDEDialog;
import de.blitzcoder.collide.util.HttpPost;
import java.awt.Window;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author blitzcoder
 */
public class SubmissionDialog extends IDEDialog {

    private ApplicationError error;
    private ErrorWindow errorWindow;

    /** Creates new form SubmissionDialog */
    public SubmissionDialog(ErrorWindow parent, ApplicationError error) {
        super(parent,"Fehler berichten");
        initComponents();

        this.errorWindow = parent;

        // email-adress may be configured
        String mail = Config.getProperty("error.submission.email");
        if (mail == null)
            mail = "";
        mailTextField.setText(mail);

        this.error = error;
        exceptionLabel.setText(error.getError().getClass().getName());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel3 = new javax.swing.JLabel();
        exceptionLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        sendButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        mailTextField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        screenshotCheckBox = new javax.swing.JCheckBox();

        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        exceptionLabel.setText("Exception");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(exceptionLabel, gridBagConstraints);

        cancelButton.setText("Abbrechen");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(cancelButton, gridBagConstraints);

        sendButton.setText("Senden");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(sendButton, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("E-Mail (Optional)"));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        mailTextField.setBorder(null);
        mailTextField.setMinimumSize(new java.awt.Dimension(0, 25));
        jPanel1.add(mailTextField);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Fehlerbeschreibung (Optional)"));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(5);
        descriptionTextArea.setBorder(null);
        jScrollPane1.setViewportView(descriptionTextArea);

        jPanel2.add(jScrollPane1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jPanel2, gridBagConstraints);

        screenshotCheckBox.setText("Screenshot mitsenden (Nur Hauptfenster)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        getContentPane().add(screenshotCheckBox, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed

        String message = error.generateEmailError();
        
        if (mailTextField.getText().length() != 0) {
            message+="\n\nemail: "+mailTextField.getText();
        }
        Config.setProperty("error.submission.email", mailTextField.getText());

        if (descriptionTextArea.getText().length() != 0) {
            message+="\n\n-------- Description:\n"+descriptionTextArea.getText();
        }

        HashMap<String,Object> map = new HashMap<String, Object>();
        map.put("subject", "CollIDE-Exception("+error.getError().getClass().getName()+")");
        map.put("message", message);

        if (screenshotCheckBox.isSelected()) {
            Interface i = Interface.get();
            if (i != null) { // Interface might not yet be created
                byte[] shot = i.screenshot();
                if (shot != null)
                    map.put("screenshot", shot);
            }
        }

        try {
            HttpPost post = new HttpPost(new URL(Config.getProperty("error.submission.scripturl")), map);
            post.startConnection();
        } catch (Exception ex) {
            throw new Error(ex);
        }

        JOptionPane.showMessageDialog(this, "Der Fehler wurde an den Autor gesendet. Vielen Dank.");

        dispose();
        errorWindow.dispose();

    }//GEN-LAST:event_sendButtonActionPerformed

    /*
     * This simple script is currently used to send the mails
     * -------------------
     *
     * <?php
     *
     *         $to = "bugs@blitz-coder.de";
     *         $from = "From: Bugscript<bugs@blitz-coder.de>";
     *
     *         if (isset($_POST['message']) && isset($_POST['subject'])) {
     *
     *              mail($to, base64_decode($_POST['subject']), base64_decode($_POST['message']),
     *                $from);
     *
     *
     *         } else {
     *             echo "Error. You have to set message and subject";
     *         }
     *
     *
     *     ?>
     *
     * -------------------
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JLabel exceptionLabel;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField mailTextField;
    private javax.swing.JCheckBox screenshotCheckBox;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables

}
